package com.nineclock.attendance.job;

import cn.hutool.core.collection.CollectionUtil;
import com.itzhanshen.sys.feign.SysUserFeign;
import com.nineclock.attendance.mapper.AttendGroupMapper;
import com.nineclock.attendance.pojo.AttendGroup;
import com.nineclock.attendance.service.AttendancePunchService;
import com.nineclock.attendance.service.AttendanceService;
import com.nineclock.dto.SysCompanyUserDTO;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Component
@Slf4j
public class SendMailTask {
    @Autowired
    AttendGroupMapper attendGroupMapper;
    @Autowired
    AttendancePunchService attendancePunchService;
    @Autowired
    SysUserFeign sysUserFeign;
    @XxlJob("countAttendanceAndSendMail")
    public void countAttendanceAndSendMail() {
        log.info("开启启动任务");
        // 获取企业ID集合
        List<Long> cIds = this.queryCompanyIds();
        // 获取每个企业考勤情况,并生成报表
        for (Long cId : cIds) {
            String filename = attendancePunchService.generateExcelReportByCompanyId(cId);
            // 获取企业主管理员
            SysCompanyUserDTO admin = sysUserFeign.queryAdminByCompanyId(cId).getData();
            System.out.println(admin.getEmail());
            String email = admin.getEmail();
            // 发送邮件
            try {
                this.sendMainAffix(email, filename);
            } catch (MessagingException e) {
                System.out.println("发送邮件失败");
                e.printStackTrace();
            }

        }
    }
// 获取企业ID集合
    private List<Long> queryCompanyIds() {
        // 从考勤组中获取
        List<AttendGroup> attendGroups = attendGroupMapper.selectList(null);
        Set<Long> sets = attendGroups.stream().map(attendGroup -> {
            return attendGroup.getCompanyId();
        }).collect(Collectors.toSet());
        return new ArrayList<>(sets);
    }

    @Value("${spring.mail.username}")
    String from;
    @Autowired
    JavaMailSender javaMailSender;
    // 发送带有附件的邮件
    public void sendMainAffix(String toEmail,String filename) throws MessagingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true);
        messageHelper.setFrom(from);
        messageHelper.setTo(toEmail);
        messageHelper.setSubject("考勤报表");
        messageHelper.setText("您本月的考勤报表已生成,请及时查收!");
        messageHelper.addAttachment("考勤报表.xls", new File(filename));
        javaMailSender.send(mimeMessage);
        System.out.println("[SUCCESS]");

    }
}
